package edu.zut.cs.software.perfect.course.service.impl;

import java.util.List;
import java.util.Optional;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.stereotype.Service;
import edu.zut.cs.software.perfect.base.service.impl.GenericTreeManagerImpl;
import edu.zut.cs.software.perfect.course.dao.CourseCategoryDao;
import edu.zut.cs.software.perfect.course.domain.CourseCategory;
import edu.zut.cs.software.perfect.course.service.CourseCategoryManager;

/**
 * @ClassName CourseCategoryManagerImpl
 * @Description TODO
 * @Author liangying
 * @Date 2020年5月29日 下午10:59:18
 * @Version 1.0
 */

@Service(value = "courseCategoryManager")
@Transactional 
public class CourseCategoryManagerImpl extends GenericTreeManagerImpl<CourseCategory, Long> implements CourseCategoryManager{

	CourseCategoryDao courseCategoryDao;

	@Autowired
	public void setCourseCategoryDao(CourseCategoryDao courseCategoryDao) {
		this.courseCategoryDao = courseCategoryDao;
		this.treeDao = this.courseCategoryDao;
		this.dao = this.treeDao;
	}
	
	@Override
	public List<CourseCategory> findAll(){
		return (List<CourseCategory>)this.dao.findAll();
	}

	

	@Override
	public Optional<CourseCategory> findByCourseId(Long courseId) {
		// TODO Auto-generated method stub
		Optional<CourseCategory> result = this.dao.findById(courseId);
		return result;
	}

//	@Override
//	public List<CourseCategory> findByCourseCategoryName(String categoryName) {
//		CourseCategory courseCategory = new CourseCategory();
//		courseCategory.setCategoryName(categoryName);
//		ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("categoryName", ExampleMatcher.GenericPropertyMatchers.startsWith()).withIgnorePaths("dateCreated", "dateModified");
//		Example<CourseCategory> example = Example.of(courseCategory, exampleMatcher);
//		List<CourseCategory> result = this.dao.findAll(example);
//		return result;
//	}
	
	
	
	@Override
	public Boolean deleteCategoryId(Long courseId) {
		// TODO Auto-generated method stub
		//为空返回Optional.empty()
		Optional<CourseCategory> courseCategory = this.dao.findById(courseId);
		if(courseCategory.equals(Optional.empty())) {
			return false;
		}else {
			this.dao.delete(courseCategory.get());
			return true;
		}
	}
}